import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STChange, STRequestOptions, STData } from '@delon/abc/st';
import { SFComponent, SFSchema } from '@delon/form';
import { SearchDrawerService } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { BasicTableComponent } from 'src/app/routes/commom';
import { TicketService } from '../../services/ticket.service';
import { TransactionDetailsComponent } from './transaction-details/transaction-details.component';

@Component({
  selector: 'app-etc-invoiced-list',
  templateUrl: './etc-invoiced-list.component.html',
  styleUrls: ['../../../commom/less/commom-table.less']
})
export class ETCInvoicedListComponent extends BasicTableComponent implements OnInit {
  columns: STColumn[] = this.initST();
  schema: SFSchema = this.initSF();

  constructor(public service: TicketService, private nzModalService: NzModalService, public searchDrawerService: SearchDrawerService) {
    super(searchDrawerService);
  }

  ngOnInit(): void {}

  search() {
    this.st?.load(1);
  }


  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf?.value
      });
    }
    return requestOptions;
  };
  afterRes = (data: any[], rawData?: any) => {
    return data.map(item => ({
      ...item,
      disabled: item.invoicingStatus !== '1' && item.invoicingStatus !== '9'
    }));     
  };
  get selectedRows() {
    return this.st?.list.filter(item => item.checked) || [];
  }
  showDetail(item: any) {
    const modal = this.nzModalService.create({
      nzTitle: '运单交易明细',
      nzContent: TransactionDetailsComponent,
      nzNoAnimation: true,
      nzWidth: 950,
      nzComponentParams: { data: item },
      nzOnOk: com => {
        console.log(com.selectedData);
      },
      nzOkText: '导出'
    });
    modal.afterClose.subscribe(res => {
      if (res) {
        this.st.load();
      }
    });
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        wayBillCode: {
          type: 'string',
          title: '运单号',
          ui: {
            autocomplete: 'off'
          }
        },
        billCode: {
          type: 'string',
          title: '订单号',
          ui: {
            autocomplete: 'off'
          }
        },
        billType: {
          type: 'string',
          title: '订单类型',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'bill:type' },
            placeholder: '请选择'
          }
        },
        invoicingStatus: {
          type: 'string',
          title: '开票状态',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'etc:invoicing:status' },
            placeholder: '请选择'
          },
          default: ''
        },
        shipperId: {
          type: 'string',
          title: '托运人',
          ui: {
            widget: 'select',
            serverSearch: true,
            searchDebounceTime: 300,
            searchLoadingText: '搜索中...',
            allowClear: true,
            onSearch: (q: any) => this.service.getEnterpriceList({ enterpriseName: q })
          }
        },
        enterpriseInfoId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder()
          },
          default: ''
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '', type: 'checkbox', fixed: 'left', width: '50px', className: 'text-center' },
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
        } 
      },
      { title: '运单号', index: 'wayBillCode', width: 200 },
      { title: '订单号', index: 'billCode', width: 200 },
      {
        title: '开票状态',
        index: 'invoicingStatusLabel',
        width: 120
      },
      { title: '订单类型', index: 'billTypeLabel', width: 120 },
      { title: '装货地', index: 'loadingPlace', width: 200 },
      { title: '卸货地', index: 'dischargePlace', width: 200 },
      { title: '司机信息', render: 'call1No', width: 200 },
      { title: '车辆信息', render: 'call12No', width: 200 },
      { title: '托运人', index: 'shipperAppUserName', width: 200 },
      { title: '网络货运人', index: 'enterpriseInfoName', width: 200 },
      {
        title: '开票金额',
        index: 'invoicingAmount',
        width: 100,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.invoicingAmount }) }
      },
      {
        title: '开票张数',
        index: 'invoicingNumber',
        width: 100,
        className: 'text-right'
      },
      { title: '申请时间', index: 'orderReceivingTime', type: 'date', width: 200 },
      {
        title: '操作',
        className: 'text-center',
        fixed: 'right',
        width: 120,
        buttons: [
          {
            text: '交易明细',
            acl: { ability: ['TICKET-ETC-INVOICE-LIST-transaction'] },
            click: item => this.showDetail(item)
          }
        ]
      }
    ];
  }
  // 导出
  exprot() {
    this.service.exportStart({ ...this.sf?.value, pageSize: -1 }, this.service.$api_get_asyncExportEtcApplyRecordList);
  }
  sureInvoice() {
    if (this.selectedRows.length <= 0) {
      this.service.msgSrv.error('请选择订单！');
      return;
    }
    let params: any[] = [];
    this.selectedRows.forEach(item => {
      params.push(item.id);
    });
    this.service.request(this.service.$api_updateEtcRecordStatus, params).subscribe(res => {
      if (res) {
        this.st.load(1);
      }
    });
  }
}
